import { getOssSignature, upload } from '@/api/modules/commom'
import { randomString } from "@/utils/util";
import { UploadRequestOptions } from 'element-plus';
export const useUpload = (options: UploadRequestOptions) => {
  const _upload = async (config = {}) => {
    const { data } : { data: any } = await getOssSignature({dir: 'files'});
    const { file } = options
    const fd = new FormData()
    let suffix = ''
    if (file.name.lastIndexOf('.') !== -1) {
      suffix = file.name.substring(file.name.lastIndexOf('.'))
    }
    const fileName = randomString(32) + suffix
    const key = data.dir + '/' + fileName
    fd.append('key', key)
    fd.append('OSSAccessKeyId', data.accessid)
    fd.append('policy', data.policy)
    fd.append('signature', data.signature)
    fd.append('file', file)
    try {
      await upload({ host: data.host, fd})
      return data.host + '/' + key
    } catch (error) {
      console.log(error)
    }
  }
  return {
    _upload
	};
}